package com.yrh.controller;

import com.yrh.bean.TAddress;
import com.yrh.bean.TUser;
import com.yrh.service.TAddressService;
import com.yrh.service.impl.TAddressServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@WebServlet("/AddressServlet/*")
public class AddressServlet extends BaseServlet {
    //创建业务层逻辑对象
    private TAddressService service=new TAddressServiceImpl();
    public void allAddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取用户的数据
        TUser user = (TUser) request.getSession().getAttribute("user");
        TAddress address=new TAddress();
        address.setUserId(user.getUserId());
        List<TAddress> aList = service.allAddress(address);
        //携带数据回去渲染
        request.setAttribute("aList",aList);
        request.getRequestDispatcher("/address.jsp").forward(request,response);
    }
    //修改默认地址
    public void  updateDefault(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String addressId = request.getParameter("addressId");
        TAddress address=new TAddress();
        service.updateDefault(address);//修改所有地址
        address.setAddressId(Integer.parseInt(addressId));
        service.updateDefault(address);//修改指定地址
        response.getWriter().println(1);
    }
    //删除地址
    public void del(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取前台请求参数
        Map<String, String[]> map = request.getParameterMap();
        TAddress address=new TAddress();
        BeanUtils.populate(address,map);
        int i = service.delAddress(address);
        if (i>0){
            response.getWriter().println(1);
        }else {
            response.getWriter().println(-1);
        }
    }
    //修改地址
    public void updateads (HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取前台数据
        Map<String, String[]> map = request.getParameterMap();
        TAddress address=new TAddress();
        BeanUtils.populate(address,map);
        //修改地址
        int i = service.updateAds(address);
        if (i>0){
            response.getWriter().println(1);
        }else {
            response.getWriter().println(-1);
        }

    }
    //添加地址
    public void addAds(HttpServletRequest request, HttpServletResponse response) throws Exception {
        TUser user = (TUser) request.getSession().getAttribute("user");//获取用户信息
        Map<String, String[]> map = request.getParameterMap();
        TAddress address=new TAddress();
        BeanUtils.populate(address,map);
        address.setUserId(user.getUserId());//设置用户
        int i = service.addAddress(address);
        if (i>0){
            response.getWriter().println(1);
        }else {
            response.getWriter().println(-1);
        }
    }
    //送餐地址
    public void shipAddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String sum = request.getParameter("sum");
        //获取用户地址
        TUser user = (TUser) request.getSession().getAttribute("user");
        TAddress address=new TAddress();
        address.setUserId(user.getUserId());
        List<TAddress> aList = service.allAddress(address);
        //携带数据回去渲染
        request.setAttribute("aList",aList);
        request.setAttribute("sum",sum);
        request.getRequestDispatcher("/order_submit.jsp").forward(request,response);
    }
}
